package in.smartbillpay.web.dao;

import in.smartbillpay.web.models.SBPUser;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public class UserDaoImpl implements UserDao {
	 protected HibernateTemplate template;

	    /**
	     * Sets Hibernate session factory.
	     */
	 @Autowired
	    public void setSessionFactory(SessionFactory sessionFactory) {
	        template = new HibernateTemplate(sessionFactory);
	    }
	 

	public SBPUser getUserById(int id) {
		return template.get(SBPUser.class,id);
	}

	@SuppressWarnings("unchecked")
	public List<SBPUser> getUserByName(String userName) {
		String query = "from SBPUser where userName=?";
		List<SBPUser> users = template.find(query,userName);
		
		return users;
	}

	

	@Transactional(readOnly = false, propagation = Propagation.NESTED)
	public SBPUser saveOrUpdate(SBPUser userDetail) {
		template.saveOrUpdate(userDetail);
		return userDetail;
	}

	
	
}
